<?php
	echo $this->filter;
?>
<div id="mediaBlockFiles">
<?php 
	echo $this->filesBlock;
?>
</div>
<?=$this->Style()->IncludeStyleFile(array('js' => array('forms')));?>
<script type="text/javascript">

$(document).ready(function() 
{
	$('.forMediaHTML, .forMediaId').focus(function(){
		activeTextAreaId = this.id;
	});

	$('#mediFilterForm').submit(function(){
	    $(this).ajaxSubmit({
        	dataType: 'json',
            success: LoadMediaFilesBlock
        });
	    return false;		
	});

	$('#mediaFilterResetButton').unbind('click').click(function(){
		$('#mediFilterForm').find(':input[type=text]').val('');	
<?php 

		$mediaLink = MozgRoute::Get('http')->Create(array_merge($this->baseUrlParams, array('resetmediafilter' => 1)));

?>
		var resetUrl='<?=$mediaLink;?>';

		$.ajax({url: resetUrl,
			dataType: 'json',
			type: 'get',
			success: LoadMediaFilesBlock
		});
	});

});

	function LoadMediaFilesBlock(data)
	{
		if (data.status == 2)
		{
			showMessage(data.message, true);
			return true;
		}

		$('#mediaBlockFiles').html(data.data.result);
		return false;
	}

	function addMediaToContent(mediaId)
	{
		var textArea = $('#'+activeTextAreaId);
		if (!textArea) return false;

		var isReplace = false;
		if (textArea.hasClass('forMediaId'))
		{
			mediaHTML = mediaId;
			isReplace = true;
		}
		else
		{
			mediaHTML = $('#mediaHTML_'+mediaId).val();
		}						
		
		if (CKEDITOR.instances[activeTextAreaId])
		{	
			CKEDITOR.instances[activeTextAreaId].insertHtml( mediaHTML );
		}			
		else if (document.getElementById(activeTextAreaId))	
		{	
			if (isReplace)
			{
				document.getElementById(activeTextAreaId).value = mediaHTML;
			}
			else
			{	
				insertAtCursor(document.getElementById(activeTextAreaId), mediaHTML);
			}	
		}	

		return false;
	};			
</script>